#include <STC89C5xRC.H>

sbit onewire_DQ=P3^7;

//初始化
unsigned char onewire_init()
{
	unsigned char i;
	unsigned char AckBit;
	onewire_DQ=1;
	onewire_DQ=0;
	i = 227;while (--i);//dealy500us
	onewire_DQ=1;
	i = 29;while (--i);//70us
	AckBit=onewire_DQ;
	i = 227;while (--i);//dealy500us
	return AckBit;
}

//发送一位数据Bit
void onewire_sendbit(unsigned char Bit)
{
	unsigned char i;
	onewire_DQ=0;
	i = 3;while (--i);//10us
	onewire_DQ=Bit;
	i = 22;while (--i);//50us
	onewire_DQ=1;
}

//接收一位数据Bit
unsigned char onewire_receivebit()
{
	unsigned char Bit;
	unsigned char i;
	onewire_DQ=0;
	i = 1;while (--i);//5us
	onewire_DQ=1;
	i = 1;while (--i);//5us
	Bit=onewire_DQ;
	i = 22;while (--i);//50us
	return Bit;
}

//发送一个字节数据byte
void onewire_sendbyte(unsigned char byte)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		onewire_sendbit(byte&(0x01<<i));
	}
}

//接收一个字节数据byte
unsigned char one_receivebyte()
{
	unsigned char i;
	unsigned char byte=0x00;

	for(i=0;i<8;i++)
	{
		if(onewire_receivebit()){byte|=(0x01<<i);}
	}
	return byte;
}


